package com.iemes.controller.jurisdiction;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.RoleFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.PositionMapper;
import com.iemes.mapper.RoleMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

/**
 * 
 * @author lanyuan 
 */
@Controller
@RequestMapping("/privilege/")
public class RoleController extends BaseController {
	@Inject
	private RoleMapper roleMapper;
	@Inject
	private PositionMapper positionMapper;
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/role/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow,
			String pageSize,HttpServletRequest request) throws Exception {
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		if (!userFormMap.getStr("site").equals("*")) {
			return null;
		}
		// 获取此账号的部门信息
		PositionFormMap positionFormMap = positionMapper.findbyFrist("id",
						userFormMap.get("position_id").toString(),
						PositionFormMap.class);
		
		List<Map<String, Object>> roleFormMap=roleMapper.getroleByDepart(positionFormMap.get("departid").toString());
		LuogicPaging(pageNow, pageSize, roleFormMap);
		return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/role/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-新增组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request) throws Exception {
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		if (!userFormMap.getStr("site").equals("*")) {
			return null;
		}
		PositionFormMap position = positionMapper.findbyFrist("id",
				userFormMap.get("position_id").toString(),
				PositionFormMap.class);
		PositionFormMap positionFormMap=getFormMap(PositionFormMap.class);
		positionFormMap.put("departid", position.get("departid"));
		positionMapper.addEntity(positionFormMap);
		RoleFormMap roleFormMap=getFormMap(RoleFormMap.class);
		roleFormMap.put("position_id", positionFormMap.get("id"));
		roleMapper.addEntity(roleFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-删除组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			roleMapper.deleteByAttribute("id", id, RoleFormMap.class);
		}
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("role", roleMapper.findbyFrist("id", id, RoleFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/system/role/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="系统管理",methods="组管理-修改组")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
		roleMapper.editEntity(roleFormMap);
		return "success";
	}
	
	
	@RequestMapping("selRole")
	public String seletRole(Model model) throws Exception {
		RoleFormMap roleFormMap = getFormMap(RoleFormMap.class);
		Object userId = roleFormMap.get("userId");
		if(null!=userId){
			List<RoleFormMap> list = roleMapper.seletUserRole(roleFormMap);
			String ugid = "";
			for (RoleFormMap ml : list) {
				ugid += ml.get("id")+",";
			}
			ugid = Common.trimComma(ugid);
			model.addAttribute("txtRoleSelect", ugid);
			model.addAttribute("userRole", list);
			if(StringUtils.isNotBlank(ugid)){
				roleFormMap.put("where", " where id not in ("+ugid+")");
			}
		}
		List<RoleFormMap> roles = roleMapper.findByWhere(roleFormMap);
		model.addAttribute("role", roles);
		return Common.BACKGROUND_PATH + "/system/user/roleSelect";
	}
	
	@RequestMapping("positionName_isExist")
	@ResponseBody
	public boolean positionName_isExist(String position_name){
		List<PositionFormMap> positionFormMaps=positionMapper.findByAttribute("position_name", position_name, PositionFormMap.class);
		if (positionFormMaps.size()>0) {
			return true;
		}
		return false;
	}
}